﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Mail;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Halide
{
	/// <summary>
	/// The Halide.EmailTemplate class reads a specified file and performs find/replace actions on it.
	/// Note: You can also specify a web-style path to the file.
	/// </summary>
	/// <example>
	/// <code>
	/// Halide.EmailTemplate tem = new EmailTemplate(Server.MapPath("/templates/" + MyEmailTemplate.htm");
	/// tem.AddFindReplaceItem("&lt;FullStreetAddress /&gt;", "This is the replacement value for FullStreetAddress");
	/// tem.PrepareAndSendEmail(...);
	/// </code>
	/// </example>
	public class EmailTemplate
	{
		private static string _version = "2008.05.31A";

		/// <summary>
		/// Return the version number of the class; read-only.
		/// </summary>
		public static string Version
		{
			get
			{
				return _version;
			}
		}

		#region Private Variables

		private string _templatePath = null;
		private string _templateContents = null;
		private string _preparedContents = null;
		private Dictionary<string, string> _findReplaceItems;

		#endregion

		#region Constructors
		/// <summary>
		/// Constructor accepts the path to a template file on disk. 
		/// </summary>
		/// <param name="templatePath"></param>
		public EmailTemplate(string templatePath)
		{
			if (File.Exists(HttpContext.Current.Server.MapPath(templatePath)))
			{
				templatePath = HttpContext.Current.Server.MapPath(templatePath);
			}

			if (!File.Exists(templatePath))
				throw new FileNotFoundException("File not found: " + templatePath);

			_templatePath = templatePath;
			_templateContents = File.ReadAllText(templatePath);
			_findReplaceItems = new Dictionary<string, string>();
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Use this to store strings that you want to find and replace in the e-mail template.
		/// </summary>
		/// <param name="find"></param>
		/// <param name="replace"></param>
		public void AddFindReplaceItem(string find, string replace)
		{
			_findReplaceItems.Add(find, replace);
		}

		/// <summary>
		/// Performs Find and Replace on the e-mail template file used in constructor, then sends the e-mail.
		/// </summary>
		/// <param name="sender">The e-mail address of the sender</param>
		/// <param name="senderName">The name of the sender</param>
		/// <param name="recipient">The e-mail of the recipient</param>
		/// <param name="ccList">List of recipients to get copied</param>
		/// <param name="bccList">List of recipients to get blind copied</param>
		/// <param name="subject">The subject of the msg</param>
		/// <param name="BodyFormat">"html" or "plaintext".</param>
		/// <returns>"SUCCESS", or an error message.</returns>
		public string PrepareAndSendMail(string sender, string senderName, string recipient, string ccList, string bccList, string subject, string BodyFormat)
		{
			FindAndReplace();
			return Halide.Email.SendMail(sender, senderName, recipient, ccList, bccList, subject, _preparedContents, BodyFormat);
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Returns the e-mail template contents after finding and replacing values that were set using AddFindReplaceItem.
		/// </summary>
		/// <returns>String - that contains the e-mail template with the "replace" values inserted</returns>
		private void FindAndReplace()
		{
			_preparedContents = _templateContents;

			foreach (string key in _findReplaceItems.Keys)
			{
				_preparedContents = _preparedContents.Replace(key, _findReplaceItems[key]);
			}
		}

		#endregion
	}
}
